#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

#include <QtNetwork>
#include<QTcpServer>//监听套接字
#include<QTcpSocket>//通信套接字
#include <QUdpSocket>
#include <QLabel>
#include <QPushButton>
#include <QComboBox>
#include <QLineEdit>
#include <QTimer>
#include <QTextEdit>
#include "dynamiccode.h"
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

typedef struct Ball_Data {
    int up_vel;              //上轮速度
    int down_vel;            //下轮速度
    float pitch_angle;         //俯仰角度
    float yaw_angle;           //左右角度
    float frequency_serve;     //频率
    quint32 Repetitions;        //重复次数
} BALL_DATA;

typedef struct Course_Data {
    quint8 serve_kind;
    quint16 serve_degree;
    quint8 serve_sort;
    float between_group_time;
    quint8 countdown;
    BALL_DATA my_ball_data[4];
} COURSE_DATA;

typedef struct DEVICE {
    char ip[4];
    char mac[6];
    char sn[12];
    char dynamic_code[6];
} MY_DEVICE;

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

private:
    Ui::Widget *ui;

    QUdpSocket *udpclient = nullptr;
    //tcp通信套接字(还有一个监听套接字，客户端只需要用到通信套接字)
    QTcpSocket *tcpclient = nullptr;


    QLabel *lb_device_list = nullptr;

    QPushButton *pB_udp_broadcast = nullptr;
    QPushButton *pB_tcp_open = nullptr;
    QPushButton *pB_tcp_close = nullptr;
    QPushButton *pB_start_serve = nullptr;

    QLabel *up_motor_speed = nullptr;
    QLabel *down_motor_speed = nullptr;
    QLabel *pitch_angle = nullptr;
    QLabel *yaw_angle = nullptr;
    QLabel *repetition = nullptr;
    QLabel *frequency = nullptr;

    QComboBox *device_select = nullptr;
    QComboBox *serve_kind_select = nullptr;
    QComboBox *serve_sort_select = nullptr;

    QLineEdit *le_up_motor_speed = nullptr;
    QLineEdit *le_down_motor_speed = nullptr;
    QLineEdit *le_pitch_angle = nullptr;
    QLineEdit *le_yaw_angle = nullptr;
    QLineEdit *le_repetition = nullptr;
    QLineEdit *le_frequency = nullptr;



    QLineEdit *le_serve_degree = nullptr;
    QLineEdit *le_group_gap = nullptr;//组间间隔

    QLabel *lb_group_gap = nullptr;


    COURSE_DATA *my_course_data;

    QLineEdit *le_countdown = nullptr;

    QLabel *lb_countdown = nullptr;//倒计时
    QPushButton *pB_skip_countdown = nullptr;//倒计时

    QTimer *time_udp_broadcast;

    QTextEdit *log;


    QDateTime *current_time;

    MY_DEVICE *device1;
    MY_DEVICE *device2;



private slots:
    void udpReceive();
    void udpBroadcast();
    void handleUdpData(QByteArray recdata);
    void tcpConnect();
    void tcpDisconnect();
    void tcpSend(QByteArray senddata);
    void tcpReceive();
    void start_serve();
    void time_udp_broadcast_out();
    void outputIp(uint8_t *ipaddress);
};
#endif // WIDGET_H
